//
//  XDHomeController.swift
//  weibo
//
//  Created by lucy on 2016/10/22.
//  Copyright © 2016年 com.huashengHuaSheng. All rights reserved.
//

import UIKit

private let statusCellIdentify = "statusCellIdentify"

class XDHomeController: XDVisitorViewController {

   let statusListViewModel = XDStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin{
        
            visitorView?.setupVisitorInfo(imageName: nil, title: nil)
            return
        
        }

        setupUI()
        getStatusData()
    }
    
    @objc private func leftBtnClick(){
        print("我左边的被点击了")
        
    }
    
    @objc private func rightBtnClick(){
        print("我右边的被点击了")
        let temp = XDTempVc()
        temp.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(temp, animated: true)
        
        
    }

    
    //MARK-视图设置
    func setupUI(){
    
        setupNav()
        setupTableViewInfo()

    }
    
    private func setupTableViewInfo(){
    
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(XDStatusCell.self, forCellReuseIdentifier: statusCellIdentify)
        tableView.estimatedRowHeight = 200
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置footView
        tableView.tableFooterView = bottomView
        refreshControl.addTarget(self, action: #selector(getStatusData), for: UIControlEvents.valueChanged)
    
    
    }
    
    //MARK-设置导航视图
    private func setupNav(){
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(setHighlightImgName:"navigationbar_pop", target: self, action: #selector(leftBtnClick))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(setHighlightImgName:"navigationbar_friendsearch", target: self, action: #selector(rightBtnClick))
    
    }
    
    //懒加载菊花控件
    fileprivate lazy var bottomView: UIActivityIndicatorView = {
    
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.purple
        return view
    
    }()
    
    //系统下拉刷新
    fileprivate lazy var refreshControl: UIRefreshControl = UIRefreshControl()
}

//MARK-请求数据
extension XDHomeController{
    func getStatusData(){
       
        statusListViewModel.getHomeData(isPullUp: bottomView.isAnimating) {(isSuccess) in
            if !isSuccess{
                
                print("请求数据失败")
               
            }else{
                self.tableView.reloadData()
            
            }
            self.endRefreshing()

        }
        
    }
    
    //停止下拉和上拉动画
    func endRefreshing(){
    
        //停止动画
        self.bottomView.stopAnimating()
        //下拉刷新也要停止
        self.refreshControl.endRefreshing()
    
    }
}

//MARK-UItableViewDataSource
extension XDHomeController:UITableViewDataSource, UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: statusCellIdentify, for: indexPath) as! XDStatusCell
        cell.statusViewModel = statusListViewModel.dataArray[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    //将要显示的cell
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == statusListViewModel.dataArray.count-1 && !bottomView.isAnimating{
        
            //开启动画
            bottomView.startAnimating()
            //马上请求数据
            getStatusData()
        }
    }
}




 





























